var picList = {};

(function(pl){
    pl.pnum = 4;
    pl.pwidth = 220;
    pl.iWidth = 218;
    pl.target = null;
    pl.tHeight = 70;
    pl.divBottom = 20;
    pl.divRight = 20;
    pl.linkHeight = 40;
    pl.hArr = [];
    pl.left = 0;
    pl.mHeight = 50;
    pl.maskobj = null;

    pl.update = function(data){
        if(typeof(data) == 'undefined'){
            return ;
        }

        if(pl.target == null){
            pl.target = document.getElementById('pic-list');
        }

        if(pl.maskobj == null){
            pl.maskobj = document.getElementById('loadlistmask');
        } 
        
        var ndiv, i=0, l=0, h = 100, k, img, ih;
        var imgs = data.i;
        var comments = data.c;
        for(k in imgs){
            img = imgs[k];
            comment = comments[img.k];
            if(typeof(pl.hArr[i]) == "undefined"){
                pl.hArr[i] = 0;
            }
            i = (i>(pl.pnum-1)?0:i);
            l = pl.left + (pl.pwidth + pl.divRight)* i ;
            ih = img.w > pl.iWidth ? Math.ceil((pl.iWidth/img.w)*img.h) : img.h;
  
            tHeight = (typeof(comment) == 'undefined') ? pl.tHeight : (pl.tHeight + 25 * (comment.n));
            h = ih + tHeight;
            idiv = ce.create('div')
                     .attribute({'class': 'img-div', 'style': {'min-height': pl.mHeight +'px', 'left': l +'px','top': pl.hArr[i] + 'px','height': h + 'px'}})
                     .appendTo(pl.target);
            iw = ((img.w > pl.iWidth) ? pl.iWidth : img.w);
            aimg = ce.create('a')
                     .attribute({'href': img.s.replace("/f/", '/d/'), 'style':{'display': 'block', 'width':iw+'px', 'height': ih+'px'}})
                     .appendTo(idiv);
            nimg = ce.create('img')
                     .attribute({'src': img.s+'?w='+ iw +'&h='+ih, 'class': 'img-self','title': (typeof(img.n) != 'undefined')?img.n:'芳草地-美女图库'})
                     //.bind('load', pl.loadImg)
                     .appendTo(aimg);
            /*nimg = ce.create('img')
                     .attribute({'src': img.s, 'class': 'img-self','title': img.n,'width': (img.w > pl.iWidth) ? pl.iWidth+'px' : img.w+'px', 'height': ih +'px' })
                     .appendTo(aimg);*/
            ldiv = ce.create('div')
                     .attribute({'class': 'info-div', 'style': {'height': '25px', 'float': 'left', 'margin': '5px 0px 5px 0px'}})
                     .appendTo(idiv);
            nimg = ce.create('img')
                     .attribute({'src': '/static/fcd/images/ilike.png', 'id': 'ilike', 'value':img.f, 'style': {'cursor': 'pointer'}, 'title': '喜欢她就支持他', 'value': img.s.replace("/f/", '/li/')})
                     .bind('click', pl.likeit)
                     .appendTo(ldiv);
            lvspan = ce.create('span')
                     .attribute({'id': 'lv'})
                     .appendTo(ldiv);
            nspan = ce.create('span')
                     .attribute({'id': 'liked'})
                     .html('喜欢她('+img.lc+')')
                     .appendTo(lvspan);
            nspan = ce.create('span')
                     .attribute({'id': 'viewed'})
                     .html('浏览了('+img.vc+')次')
                     .appendTo(lvspan);
            nspan = ce.create('span')
                     .attribute({'id': 'comments'})
                     .html('评论(<a href="'+img.s.replace("/f/", '/d/')+'#comments">'+(typeof(comment) == 'undefined'?0:comment.n)+'</a>)')
                     .appendTo(ldiv);
            cdiv = ce.create('div')
                     .attribute({'class': 'comment-div', 'style': {'min-height': '15px', 'float': 'left', 'margin': '5px 0px 5px 0px'}})
                     .appendTo(idiv);
            if(typeof(comment) == 'undefined' || comment.n == 0){
                nspan = ce.create('span')
                     .attribute({'id': 'no-comments'})
                     .html('暂时没有评论,(<a href="'+img.s.replace("/f/", '/d/')+'#addcomments">抢沙发</a>)')
                     .appendTo(cdiv);
            }
            else{
                var k, v;
                for(k in comment.d){
                    v = comment.d[k];
                    ccdiv = ce.create('div')
                              .attribute({'class': 'cm-div', 'style': {'min-height': '25px', 'width': '100%', 'padding': '5px 0px 5px 0px', 'border-bottom':'1px solid #f2f2f2'}})
                              .appendTo(cdiv);
                    avimg = ce.create('img')
                              .attribute({'src': 'http://www.gravatar.com/avatar/'+v.av, 'title': v.un, 'style':{'float':'left'}})
                              .appendTo(ccdiv);
                    ccspan = ce.create('span')
                               .attribute({'id': 'ccontents', 'style':{'float':'left'}})
                               .html('<span class="short-coms"><a href="'+v.ur+'" target="_blank">'+v.un+'</a>:'+v.ct+'</span><span style="color:#CCC">'+v.tt+'</span>')
                               .appendTo(ccdiv);
                }
            }
            pl.hArr[i] += h + pl.divBottom;
            pl.setTargetHeight(pl.hArr[i] + pl.linkHeight);
            i++;
        }

        ce.setobj(pl.maskobj)
          .hiddenMask()
          .clear();
    };

    /*pl.loadImg = function(){
        $(this).attr('src', '/static/fcd/images/loadpic.png');
    }*/

    pl.likeit = function(){
       var self = this;
       $.get($(self).attr('value').replace("/li/", '/ul/'), {'t': (new Date()).getTime()}, function(data){
           var d = $.parseJSON(data);
           data = null;
            switch(d[0]){
                case 'E_UPDATE_SUCCESSED':
                    alert('喜欢她，就爱她！');
                    var liked = $(self).next("span[id='lv']").children("span [id='liked']");
                    liked.text('喜欢她('+d[1]+')');
                break;
                case 'E_LIKED':
                    alert('距离产生美，点击一次够。');
                break;
                case 'E_IMG_NOT_EXISTS':
                    alert('图片未找到！');
                break;
                default:
                    alert('你做了什么？我不想理你了。');
            }
       });
    };

    pl.setTargetHeight = function(h){
        if($(pl.target).height() < h){
            $(pl.target).height(h);
        }
    };
})(picList);

var ce = {};
(function(e){
    e.create = function(type){
        e.obj = document.createElement(type);

        return e;
    };

    e.setobj = function(obj){
        if(typeof(obj) != 'undefined'){
            e.obj = obj;
        }
        return e;
    };

    e.clear = function(){
        e.obj = null;
    };

    e.appendTo = function(target){
        if(typeof(target) != 'undefined' && target != null){
            target.appendChild(e.obj);
        }
        return e.obj;
    };

    e.bind = function(t, c){
        switch(t){
            case 'load':
                 $(e.obj).load(c);
            break;
            default:
                $(e.obj).bind(t, c);
        }
        return e;
    };

    e.showMask = function(){
        $(e.obj).css('top', ($(document).height()-500) + 'px');
        $(e.obj).css('left', (($(document).width()-380)/2) + 'px');
        $(e.obj).css('display', 'block');
        return e;
    };

    e.hiddenMask = function(){
        $(e.obj).css('display', 'none');
        return e;
    };

    e.append = function(source){
        if(typeof(source) != 'undefined' && source != null){
            e.obj.appendChild(source);
        }
        return e.obj;
    };

    e.html = function(text){
        e.obj.innerHTML = text;

        return e;
    }

    e.attribute =function(attr){
        if(typeof(attr) != 'undefined'){
            for(k in attr){
                var v = attr[k];
                switch(k){
                    case 'style':
                       var s = "";
                        for(k in v){
                            s += k+": " + v[k] + ';';
                        }
                        e.obj.setAttribute('style', s);
                    break;
                    default:
                        e.obj.setAttribute(k, v);
                }
            }
        }
        return e;
    };
})(ce);

var fcd = {};
(function(f){
    f.mt = 3;
    f.t = 1;
    f.p = 1;
    f.getPic = function(p, i){
        f.p = (typeof(p)=='undefined')?1:p;
        i = (typeof(i)=='undefined')?1:i;
        
        ce.setobj(document.getElementById('loadlistmask'))
          .showMask()
          .clear();
        if(typeof(_ab) == 'undefined'){
            var url = '/il/_ALL_/';
        }
        else{
            var url = '/il/' + _ab + '/';
        }
        $.get(url, {'p':p,'i':i}, function(data){
            /*var d = $.parseJSON(data);
            console.log(d);*/
            picList.update(data);
        }, 'json');
        f.t++;
    };

    f.init = function(){
       if(f.isBottom() == true && f.mt >= f.t){
          f.getPic(f.p, f.t);
       }
    };

    f.isBottom = function(){
        if($(window).scrollTop() == $(document).height() - $(window).height()) {  
            return true; 
        } else {  
            return false;
        }  
    };
})(fcd);

$(document).ready(function(){
    window.onscroll = fcd.init;
    fcd.getPic(_p);
});